﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VisaApp.Helper;
using Visa.Common.MvcContronller;
using Visa.Service;
using VisaApp.Models;
using Visa.DB;
using System.IO;

namespace VisaApp.Controllers
{
    public class NewsController : UTController
    {
        private VisaService VisaSer = new VisaService();
        private QuocGiaService ChaulucSer = new QuocGiaService();
        private ThongtinhuuService thongtinhuuichSv = new ThongtinhuuService();
        private NewsService NewsSer = new NewsService();
        private StringHelpers adminHelper = new StringHelpers();
        private TopicService TopicSer = new TopicService();
        private ThongtinhuuService ttHuSv = new ThongtinhuuService();
        private EmailResgeditSv EmailresSv = new EmailResgeditSv();
        private YkienkhachanhService yKienSv = new YkienkhachanhService();
        private CauhoiService CauhoiSv = new CauhoiService();
        private HotrotructuyenService HTTSv = new HotrotructuyenService();
        private TuyendungService TuyenDungSv = new TuyendungService();
        private DownloadService ThuVienSv = new DownloadService();
        private MenuService MenuServices = new MenuService();
        private int PageSize = 10;

        //

        // GET: /News/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetThongtinHuuIchHomePage()
        {
            var c = ttHuSv.GetAllThongtinhuich().ToList();
            List<ThongtinhuuichModel> model = new List<ThongtinhuuichModel>();

            if (c.Count() > 0)
            {
                if (c.Count() > 15) c = c.Take(15).ToList();

                foreach (var g in c)
                {
                    ThongtinhuuichModel ml = new ThongtinhuuichModel();
                    ThongtinhuuichModel.MapFrom(g, ref ml);
                    model.Add(ml);
                }

            }
            return PartialView("_GetThongtinHuuIchHomePage", model);
        }

        public ActionResult LisstThongtinHuuich(string regioncode, int id, int page)
        {
            var c = ttHuSv.GetListTTHIByStringAndId(regioncode, id).ToList();
            List<ThongtinhuuichModel> model = new List<ThongtinhuuichModel>();

            if (c.Count() > 0)
            {
                if (c.Count() > 15) c = c.Take(15).ToList();

                foreach (var g in c)
                {
                    ThongtinhuuichModel ml = new ThongtinhuuichModel();
                    ThongtinhuuichModel.MapFrom(g, ref ml);
                    model.Add(ml);
                }

            }

            var ListNewsPageSize = new PagedData<ThongtinhuuichModel>();

            if (model.Count() > 0)
            {
                ListNewsPageSize.Data = model.Skip(PageSize * (page - 1)).Take(PageSize).ToList();
                ListNewsPageSize.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)model.Count() / PageSize));
                ListNewsPageSize.CurrentPage = page;
            }
            else
            {
                ListNewsPageSize.Data = null;
                ListNewsPageSize.NumberOfPages = 0;
                ListNewsPageSize.CurrentPage = 0;
            }

            ViewBag.regioncode = regioncode;
            ViewBag.id = id;

            return PartialView("_LisstThongtinHuuich", ListNewsPageSize);
        }

        public ActionResult ListOnthongtinHuuich(int id, int page)
        {
            var c = ttHuSv.GetListTTHiALl(id).ToList();
            List<ThongtinhuuichModel> model = new List<ThongtinhuuichModel>();

            if (c.Count() > 0)
            {
                foreach (var g in c)
                {
                    ThongtinhuuichModel ml = new ThongtinhuuichModel();
                    ThongtinhuuichModel.MapFrom(g, ref ml);
                    model.Add(ml);
                }

            }

            var ListNewsPageSize = new PagedData<ThongtinhuuichModel>();

            if (model.Count() > 0)
            {
                ListNewsPageSize.Data = model.Skip(PageSize * (page - 1)).Take(PageSize).ToList();
                ListNewsPageSize.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)model.Count() / PageSize));
                ListNewsPageSize.CurrentPage = page;
            }
            else
            {
                ListNewsPageSize.Data = null;
                ListNewsPageSize.NumberOfPages = 0;
                ListNewsPageSize.CurrentPage = 0;
            }
            return PartialView("_ListOnthongtinHuuichOnpage", ListNewsPageSize);
        }

        public ActionResult ThongtinhuuichDetilas(string name, int id)
        {
            var c = ttHuSv.GetThongtinhuichById(id);
            ThongtinhuuichModel ml = new ThongtinhuuichModel();
            ThongtinhuuichModel.MapFrom(c, ref ml);
            return View(ml);
        }

        public ActionResult ListThongtinHuuichKhac(int id, int page)
        {
            var cId = ttHuSv.GetThongtinhuichById(id);
            var c = ttHuSv.GetListTTHIByStringAndId(cId.Regioncode, cId.QuocGia.Value).Where(o => o.Id != id).ToList() ;
            List<ThongtinhuuichModel> model = new List<ThongtinhuuichModel>();

            if (c.Count() > 0)
            {
                foreach (var g in c)
                {
                    ThongtinhuuichModel ml = new ThongtinhuuichModel();
                    ThongtinhuuichModel.MapFrom(g, ref ml);
                    model.Add(ml);
                }

            }
            else
            {
                var h = ttHuSv.GetListTTHIByStringAndId(cId.Regioncode, -1).Where(o => o.Id != id).ToList() ;
                if (h.Count() > 0)
                {
                    foreach (var g in h)
                    {
                        ThongtinhuuichModel ml = new ThongtinhuuichModel();
                        ThongtinhuuichModel.MapFrom(g, ref ml);
                        model.Add(ml);
                    }
                }
            }

            var ListNewsPageSize = new PagedData<ThongtinhuuichModel>();

            if (model.Count() > 0)
            {
                ListNewsPageSize.Data = model.Skip(PageSize * (page - 1)).Take(PageSize).ToList();
                ListNewsPageSize.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)model.Count() / PageSize));
                ListNewsPageSize.CurrentPage = page;
            }
            else
            {
                ListNewsPageSize.Data = null;
                ListNewsPageSize.NumberOfPages = 0;
                ListNewsPageSize.CurrentPage = 0;
            }

            ViewBag.ID = id;

            return PartialView("_ListThongtinHuuichKhac", ListNewsPageSize);
        }

        public ActionResult GetCauHoiHomePage()
        {
            var c = CauhoiSv.GetAll().Where( o => o.Status == true).ToList();
            List<CauhoiModel> model = new List<CauhoiModel>();

            if (c.Count() > 0)
            {
                if (c.Count() > 15) c = c.Take(15).ToList();

                foreach (var g in c)
                {
                    CauhoiModel ml = new CauhoiModel();
                    CauhoiModel.mapFrom(g, ref ml);
                    model.Add(ml);
                }

            }
            return PartialView("_GetCauHoiHomePage", model);
        }

        public ActionResult GetCauHoiOncate(int id, int page)
        {
            var c = CauhoiSv.GetAll().Where(o => o.Status == true && ( o.QuocgiaID == id || id == -1)).ToList();
            List<CauhoiModel> model = new List<CauhoiModel>();

            if (c.Count() > 0)
            {
                foreach (var g in c)
                {
                    CauhoiModel ml = new CauhoiModel();
                    CauhoiModel.mapFrom(g, ref ml);

                    var stringTraloi = CauhoiSv.GetTraloiByCauhoiId(g.Id);

                    if (stringTraloi != null)
                    {
                        ml.Traloi = stringTraloi.TraloiNoidung;
                    }
                    model.Add(ml);
                }

            }

            var ListNewsPageSize = new PagedData<CauhoiModel>();

            if (model.Count() > 0)
            {
                ListNewsPageSize.Data = model.Skip(PageSize * (page - 1)).Take(PageSize).ToList();
                ListNewsPageSize.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)model.Count() / PageSize));
                ListNewsPageSize.CurrentPage = page;
            }
            else
            {
                ListNewsPageSize.Data = null;
                ListNewsPageSize.NumberOfPages = 0;
                ListNewsPageSize.CurrentPage = 0;
            }

            return PartialView("_GetCauHoiOncate", ListNewsPageSize);
        }

        public ActionResult CauhoiByQuocGiaID(string Text)
        {
            var c = CauhoiSv.GetAllBYQuocGia(Text);
            List<CauhoiModel> model = new List<CauhoiModel>();

            if (c.cauhoientity.Count() > 0)
            {
                foreach (var g in c.cauhoientity)
                {
                    CauhoiModel ml = new CauhoiModel();
                    CauhoiModel.mapFrom(g, ref ml);
                    model.Add(ml);
                }

            }
            return PartialView("_CauhoiByQuocGiaID", model);
        }


        public ActionResult CauHoiDetails(string name, int id)
        {
            CauhoiModel model = new CauhoiModel();
            var c = CauhoiSv.GetById(id);
            CauhoiModel.mapFrom(c, ref model);
            var stringTraloi = CauhoiSv.GetTraloiByCauhoiId(id);
            if (stringTraloi != null)
            {
                model.Traloi = stringTraloi.TraloiNoidung;
            }
            return View(model);
        }

        public ActionResult GetCauhoilienquan(int id)
        {
            var cs = CauhoiSv.GetById(id);
            var c = CauhoiSv.GetCauhoiByQuocGiaOrRegon(cs.regioncode).Where(u => u.Id != id).ToList();
            List<CauhoiModel> model = new List<CauhoiModel>();

            if (c.Count() > 0)
            {
                foreach (var g in c)
                {
                    CauhoiModel ml = new CauhoiModel();
                    CauhoiModel.mapFrom(g, ref ml);
                    model.Add(ml);
                }

            }
            return PartialView("_GetCauhoilienquan", model);
        }


        public ActionResult GetNewsHomePage()
        {
            var c = NewsSer.GetAllNews().Where( o => o.ALowHome == true).ToList();
            List<NewsModel> model = new List<NewsModel>();

            if (c.Count() > 0)
            {
                if (c.Count() > 10) c = c.Take(10).ToList();

                foreach (var g in c)
                {
                    NewsModel ml = new NewsModel();
                    NewsModel.MapForm(g, ref ml);
                    model.Add(ml);
                }

            }
            return PartialView("_GetNewsHomePage", model);
        }

        public ActionResult GetNewsHomePageOnCate(int page)
        {
            var c = NewsSer.GetAllNews().ToList();
            List<NewsModel> model = new List<NewsModel>();

            if (c.Count() > 0)
            {

                foreach (var g in c)
                {
                    NewsModel ml = new NewsModel();
                    NewsModel.MapForm(g, ref ml);
                    model.Add(ml);
                }

            }

            var ListNewsPageSize = new PagedData<NewsModel>();

            if (model.Count() > 0)
            {
                ListNewsPageSize.Data = model.Skip(PageSize * (page - 1)).Take(PageSize).ToList();
                ListNewsPageSize.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)model.Count() / PageSize));
                ListNewsPageSize.CurrentPage = page;
            }
            else
            {
                ListNewsPageSize.Data = null;
                ListNewsPageSize.NumberOfPages = 0;
                ListNewsPageSize.CurrentPage = 0;
            }

            return PartialView("_GetNewsHomePageOnCate", ListNewsPageSize);
        }

        public ActionResult NewsDetails(string name, int id)
        {
            var c = NewsSer.GetNewsById(id);
            NewsModel ml = new NewsModel();
            NewsModel.MapForm(c, ref ml);
            return View(ml);
        }

        public ActionResult Newslistkhac(int id, int page)
        {
            var c = NewsSer.GetAllNews().Where( o => o.Id != id).ToList();
            List<NewsModel> model = new List<NewsModel>();

            if (c.Count() > 0)
            {

                foreach (var g in c)
                {
                    NewsModel ml = new NewsModel();
                    NewsModel.MapForm(g, ref ml);
                    model.Add(ml);
                }

            }

            var ListNewsPageSize = new PagedData<NewsModel>();

            if (model.Count() > 0)
            {
                ListNewsPageSize.Data = model.Skip(PageSize * (page - 1)).Take(PageSize).ToList();
                ListNewsPageSize.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)model.Count() / PageSize));
                ListNewsPageSize.CurrentPage = page;
            }
            else
            {
                ListNewsPageSize.Data = null;
                ListNewsPageSize.NumberOfPages = 0;
                ListNewsPageSize.CurrentPage = 0;
            }
            ViewBag.ID = id;
            return PartialView("_Newslistkhac", ListNewsPageSize);
        }

        public ActionResult GetYkienKhachangHomePage()
        {
            var c = yKienSv.GetAll().Where( p => p.Status == true).ToList();
            List<YkienkhachhangModel> model = new List<YkienkhachhangModel>();

            if (c.Count() > 0)
            {
                if (c.Count() > 15) c = c.Take(15).ToList();

                foreach (var g in c)
                {
                    YkienkhachhangModel ml = new YkienkhachhangModel();
                    YkienkhachhangModel.MapFrom(g, ref ml);
                    model.Add(ml);
                }

            }
            return PartialView("_GetYkienKhachangHomePage", model);
        }


        public ActionResult GetYkienKhachangOncate(int page)
        {
            var c = yKienSv.GetAll().Where(p => p.Status == true).ToList();
            List<YkienkhachhangModel> model = new List<YkienkhachhangModel>();

            if (c.Count() > 0)
            {
                foreach (var g in c)
                {
                    YkienkhachhangModel ml = new YkienkhachhangModel();
                    YkienkhachhangModel.MapFrom(g, ref ml);
                    model.Add(ml);
                }

            }

            var ListNewsPageSize = new PagedData<YkienkhachhangModel>();

            if (model.Count() > 0)
            {
                ListNewsPageSize.Data = model.Skip(PageSize * (page - 1)).Take(PageSize).ToList();
                ListNewsPageSize.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)model.Count() / PageSize));
                ListNewsPageSize.CurrentPage = page;
            }
            else
            {
                ListNewsPageSize.Data = null;
                ListNewsPageSize.NumberOfPages = 0;
                ListNewsPageSize.CurrentPage = 0;
            }

            return PartialView("_GetYkienKhachangOncate", ListNewsPageSize);
        }

        public ActionResult TuvantrucTuyen()
        {
            var c = HTTSv.GetAll().ToList();
            List<HotrotructuyenModel> model = new List<HotrotructuyenModel>();

            if (c.Count() > 0)
            {
                if (c.Count() > 15) c = c.Take(15).ToList();

                foreach (var g in c)
                {
                    HotrotructuyenModel ml = new HotrotructuyenModel();
                    HotrotructuyenModel.Mapfrom(g, ref ml);
                    model.Add(ml);
                }

            }
            return PartialView("_TuvantrucTuyen", model);
        }


        public ActionResult EmailRes()
        {
            EmailResModel model = new EmailResModel();
            return PartialView("_EmailRes", model);
        }

        [HttpPost]
        public ActionResult EmailRes(EmailResModel model)
        {
            if (string.IsNullOrEmpty(model.Email)) 
            {
                return JsonError(" Vui lòng nhập Email của bạn vào");
            }
            if (!IsValidEmailAddress(model.Email))
            {
                return JsonError(" Email bạn nhập vào sai định dạng, vui lòng xem lại");
            }
            if (EmailresSv.CheckEmail(model.Email))
            {
                return JsonError(" Email này đã được đăng ký rùi");
            }

            model.DateCreate = DateTime.Now;
            model.Status = true;
            model.id = -1;
            EmailResgedit entity = new EmailResgedit();
            EmailResModel.MapFrom(model, ref entity);
            EmailresSv.addOrUpdateNews(entity);
            return JsonSuccess(null, null);
        }

        public ActionResult EmailGoiCauHoi()
        {
            CauhoiModel model = new CauhoiModel();
            return PartialView("_EmailGoiCauHoi", model);
        }

        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post), ValidateInput(false)]
        public ActionResult EmailGoiCauHoi(CauhoiModel model)
        {
            if (string.IsNullOrEmpty(model.Title))
            {
                return JsonError(" Vui lòng nhập tiêu đề câu hỏi");
            }
            if (string.IsNullOrEmpty(model.Name))
            {
                return JsonError(" Vui lòng nhập họ tên của bạn vào");
            }
            if (string.IsNullOrEmpty(model.Email))
            {
                return JsonError(" Vui lòng nhập Email của bạn vào");
            }
            if (!IsValidEmailAddress(model.Email))
            {
                return JsonError(" Email bạn nhập vào sai định dạng, vui lòng xem lại");
            }
            if (string.IsNullOrEmpty(model.Noidung))
            {
                return JsonError("Vui lòng nhập nội dung vào của bạn vào");
            }

            model.DateCreate = DateTime.Now;
            model.Status = false;
            model.Id = -1;
            model.Answered = false;
            model.Detele = false;
            Cauhoi entity = new Cauhoi();
            CauhoiModel.mapFrom(model, ref entity);
            CauhoiSv.addOrUpdateNews(entity);
            return JsonSuccess(null, null);
        }

        public ActionResult EmailGoiykien()
        {
            YkienkhachhangModel model = new YkienkhachhangModel();
            return PartialView("_EmailGoiykien", model);
        }

        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post), ValidateInput(false)]
        public ActionResult EmailGoiykien(YkienkhachhangModel model)
        {
            if (string.IsNullOrEmpty(model.title))
            {
                return JsonError(" Vui lòng nhập tiêu đề ý kiến");
            }

            if (string.IsNullOrEmpty(model.UserName))
            {
                return JsonError(" Vui lòng nhập họ tên của bạn");
            }

            if (string.IsNullOrEmpty(model.Email))
            {
                return JsonError(" Vui lòng nhập Email của bạn vào");
            }

            if (!IsValidEmailAddress(model.Email))
            {
                return JsonError(" Email bạn nhập vào sai định dạng, vui lòng xem lại");
            }

            //if (yKienSv.checkEmail(model.Email))
            //{
            //    return JsonError(" Email này đã được đăng ký rùi");
            //}

            if (string.IsNullOrEmpty(model.NoiDung))
            {
                return JsonError(" Vui lòng nhập nội dung ý kiến vào");
            }


            model.DateCreate = DateTime.Now;
            model.Status = false;
            model.Detele = false;
            model.Id = -1;
            Ykienkhachang entity = new Ykienkhachang();
            YkienkhachhangModel.MapFrom(model, ref entity);
            yKienSv.addOrUpdateNews(entity);
            return JsonSuccess(null, null);
        }


        public ActionResult LisTuyenDungOncate()
        {
            var c = TuyenDungSv.GetAllNews();
            List<TuyendungModel> model = new List<TuyendungModel>();

            if (c.Count() > 0)
            {
                foreach (var g in c)
                {
                    TuyendungModel ml = new TuyendungModel();
                    TuyendungModel.MapFrom(g, ref ml);
                    model.Add(ml);
                }

            }

            return PartialView("_LisTuyenDungOncate", model);
        }

        public ActionResult ListTuyenDungLienQuan(int id)
        {
            var c = TuyenDungSv.GetAllNews().Where( l => l.ID != id).ToList();
            List<TuyendungModel> model = new List<TuyendungModel>();

            if (c.Count() > 0)
            {
                foreach (var g in c)
                {
                    TuyendungModel ml = new TuyendungModel();
                    TuyendungModel.MapFrom(g, ref ml);
                    model.Add(ml);
                }

            }

            return PartialView("_ListTuyenDungLienQuan", model);
        }

        public ActionResult TuyenDungDetails(string name, int id)
        {
            var c = TuyenDungSv.GetNewsById(id);
            TuyendungModel ml = new TuyendungModel();
            TuyendungModel.MapFrom(c, ref ml);
            return View("TuyenDungDetails", ml);
        }

        public ActionResult ListThuVienOnCate()
        {
            var c = ThuVienSv.GetAllNews();
            List<DownloadModel> model = new List<DownloadModel>();
            if (c != null)
            {
                c.ForEach(delegate(Download tt)
                {
                    DownloadModel ml = new DownloadModel()
                    {
                        FileName = tt.FileName,
                        Id = tt.Id,
                        UrlFile = tt.UrlFile,
                        DateCreate = tt.DateCreate,
                    };
                    model.Add(ml);
                });
            }

            return PartialView("_ListThuVienOnCate", model);
        }


        public ActionResult Download(int id)
        {
            try
            {
                var c = ThuVienSv.GetNewsById(id);
                string fileName = adminHelper.RemoveSign4VietnameseString(c.FileName);
                string filePath = System.IO.Path.Combine(Request.PhysicalApplicationPath, "Content\\FileDownload", c.UrlFile);
                string Extion = Path.GetExtension(filePath);
                var bytes = System.IO.File.ReadAllBytes(filePath);
                return File(bytes, "text/" + Extion.Replace(".",""), fileName + Extion);
            }
            catch
            {
                return RedirectToAction("Thuvien", "Home");
            }
        }

        public ActionResult GeTinOFmenuOnCate(int page, int cate)
        {
            var c = MenuServices.GetALlByCateMenuId(cate);
            List<TinOfMenuModel> model = new List<TinOfMenuModel>();

            if (c.Count() > 0)
            {

                foreach (var g in c)
                {
                    TinOfMenuModel ml = new TinOfMenuModel();
                    TinOfMenuModel.MapForm(g, ref ml);
                    model.Add(ml);
                }

            }

            ViewBag.cateCode = cate;

            var ListNewsPageSize = new PagedData<TinOfMenuModel>();

            if (model.Count() > 0)
            {
                ListNewsPageSize.Data = model.Skip(PageSize * (page - 1)).Take(PageSize).ToList();
                ListNewsPageSize.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)model.Count() / PageSize));
                ListNewsPageSize.CurrentPage = page;
            }
            else
            {
                ListNewsPageSize.Data = null;
                ListNewsPageSize.NumberOfPages = 0;
                ListNewsPageSize.CurrentPage = 0;
            }

            return PartialView("_GeTinOFmenuOnCate", ListNewsPageSize);
        }

        public ActionResult TinOfmenuDetails(string name, int id)
        {
            TinofMenu news = MenuServices.GetTinOfMenuById(id);
            var model = new TinOfMenuModel();
            TinOfMenuModel.MapForm(news, ref model);
            return View(model);
        }

        #region Images

        public string UploadImage(HttpPostedFileBase file)
        {
            string basePath = "~/Content/imagesNews";
            FileHelper fileHelper = new FileHelper(basePath);
            string fileName = fileHelper.SaveFile(null, file.FileName, file.InputStream);
            string filePathStr = Url.Content(basePath) + @"/" + fileName;
            return filePathStr;
        }

        [HttpPost]
        public ActionResult UploadImageStr(string qqfile)
        {
            string basePath = "~/Content/imagesNews";
            string[] fileNameArray = qqfile.Split(new char[] { '.' });
            if (fileNameArray.Length == 1)
            {
                return JsonError("lỗi");
            }
            FileHelper fileHelper = new FileHelper(basePath);
            string fileName = fileHelper.SaveFile(null, fileNameArray[1], Request.InputStream);
            string filePathStr = Url.Content(basePath) + @"/" + fileName;
            return Json(new { success = true, filePath = Url.Content(basePath) + @"/" + fileName });
        }

        [HttpPost]
        public ActionResult UploadImageStrFIle(string qqfile)
        {
            string basePath = "~/Content/FileDownload";
            string[] fileNameArray = qqfile.Split(new char[] { '.' });
            if (fileNameArray.Length == 1)
            {
                return JsonError("lỗi");
            }
            FileHelper fileHelper = new FileHelper(basePath);
            string fileName = fileHelper.SaveFile(null, fileNameArray[1], Request.InputStream);
            string filePathStr = Url.Content(basePath) + @"/" + fileName;
            return Json(new { success = true, filePath = fileName });
        }

        [HttpPost]
        public ActionResult UploadImageStrBanner(string qqfile)
        {
            string basePath = "~/Content/FlashAds";
            string[] fileNameArray = qqfile.Split(new char[] { '.' });
            if (fileNameArray.Length == 1)
            {
                return JsonError("lỗi");
            }
            FileHelper fileHelper = new FileHelper(basePath);
            string fileName = fileHelper.SaveFile(null, fileNameArray[1], Request.InputStream);
            string filePathStr = Url.Content(basePath) + @"/" + fileName;
            return Json(new { success = true, filePath = Url.Content(basePath) + @"/" + fileName });
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UploadImage(HttpPostedFileBase upload, string CKEditorFuncNum,
                                      string CKEditor, string langCode)
        {
            string url = string.Empty; // url to return
            string message = string.Empty; // message to display (optional)

            // here logic to upload image
            // and get file path of the image

            // path of the image
            string path = UploadImage(upload);

            // will create http://localhost:1457/Content/Images/my_uploaded_image.jpg

            url = Request.Url.GetLeftPart(UriPartial.Authority) + path;

            // passing message success/failure
            message = "Image was saved correctly";

            // since it is an ajax request it requires this string
            string output = @"<html><body><script>window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ", \"" + url + "\", \"" + message + "\");</script></body></html>";
            return Content(output);
        }

        #endregion
    }
}
